﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Data.Sql;
using LR4.Models;

namespace LR4.DAO
{
    public class AutoUs : DAO
    {

        public User GetUser(User user2)  //Получение: Для авторизации пользователей
        {
            Connect();
            User u = new User();
            try
            {
                SqlCommand command = new SqlCommand("SELECT * FROM [User] WHERE login = @login", Connection);
                command.Parameters.AddWithValue("login", user2.login);
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    if (Convert.ToString(reader["password"]) == user2.password)
                    {
                        u.login = Convert.ToString(reader["login"]);
                        u.password = Convert.ToString(reader["password"]);
                        u.role = Convert.ToString(reader["role"]);
                    }
                }
                reader.Close();
            }
            catch (Exception e)
            {
                //Error
                Console.Write("Ошибка:" + e.Message);
            }
            finally
            {
                Disconnect();
            }
            return u;
        }

        public List<User> GetAllUsers()  //Получение: Список всех пользователей
        {
            Connect();
            List<User> UserList = new List<User>();
            try
            {
                SqlCommand command = new SqlCommand("SELECT * FROM [User]", Connection);
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    User user = new User();
                    user.login = Convert.ToString(reader["login"]);
                    user.password = Convert.ToString(reader["password"]);
                    user.role = Convert.ToString(reader["role"]);
                    UserList.Add(user);
                }
                reader.Close();
            }
            catch (Exception e)
            {
                //Error
                Console.Write("Ошибка:" + e.Message);
            }
            finally
            {
                Disconnect();
            }
            return UserList;
        }


        public bool EditUser(User user)  //Редактирование: изменить пользователя
        {
            bool result = true;
            Connect();

            try
            {
                SqlCommand edit = new SqlCommand("UPDATE [User] SET login=@login, password=@password, " +
                    "role=@role " +
                "WHERE (login=@login)", Connection);
                edit.Parameters.AddWithValue("@login", user.login);
                edit.Parameters.AddWithValue("@password", user.password);
                edit.Parameters.AddWithValue("@role", user.role);
                edit.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                //Error
                Console.Write("Ошибка:" + e.Message);
                result = false;
            }
            finally
            {
                Disconnect();
            }
            return result;
        }

        public User GetUser(String login)  //Получение:  Пользователя
        {
            Connect();
            User user = new User();
            try
            {
                SqlCommand command = new SqlCommand("SELECT * FROM [User] WHERE login = @login", Connection);
                command.Parameters.AddWithValue("@login", login);
                SqlDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    user.login = Convert.ToString(reader["login"]);
                    user.password = Convert.ToString(reader["password"]);
                    user.role = Convert.ToString(reader["role"]);
                 }
                reader.Close();
            }
            catch (Exception e)
            {
                //Error
                Console.Write("Ошибка:" + e.Message);
            }
            finally
            {
                Disconnect();
            }
            return user;
        }


        public void DeleteUser(String login)  //Удаление: удалить пользователя
        {
            Connect();
            try
            {
                SqlCommand del = new SqlCommand("DELETE FROM [User] WHERE ( login = @login )", Connection);
                del.Parameters.Add(new SqlParameter("@login", login));
                del.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                //Error
                Console.Write("Ошибка:" + e.Message);
            }
            finally
            {
                Disconnect();
            }
        }

        public bool AddUser(User user)  //Создание: добавить пользователя
        {
            bool result = true;
            Connect();
            try
            {
                SqlCommand addUser = new SqlCommand("INSERT INTO [User] (" +
                "login, password) " +
                "VALUES (@login, @password)", Connection);
                addUser.Parameters.Add(new SqlParameter("@login", user.login));
                addUser.Parameters.Add(new SqlParameter("@password", user.password));
                addUser.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                //Error
                result = false;
                Console.Write("Ошибка:" + e.Message);
            }
            finally
            {
                Disconnect();
            }
            return result;
        }

    }
    }

